package simtester.server



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
 */
@TestFor(InstanceController)
@Mock([User,TestCase,TestRun,Instance,Team])
class InstanceControllerTests {
    def testusername = 'testuser'
    def testcasename = 'tc1'
    def testcasescript = 'some script'
    def testrun
    
    @Before
    void setUp() {
        def u = new User(username: 'testuser', password: 'pass')
        def team = new Team(name: 'svt')
        team.addToUsers(u)

        def testcase = new TestCase(name: 'tc1', owner: u, script: 'some script')
        team.addToTestcases(testcase)
        team.save(failOnError: true)

        testrun = new TestRun(testcase: testcase, status: 'running')
        testrun.save(failOnError: true)
    }

    @Test
    void listInstances() {
        def cloudIds = ['i-39555b7e', 'i-39555b7f']
        cloudIds.each {
            new Instance(testrun: testrun,
                         cloudId: it,
                         amiId:   'ami-1bd68a5e',
                         type:    'm1.large',
                         status:  'free').save(failOnError:true)
        }
        
        request.method = 'GET'
        session.user = 'linh'
        def model = controller.list()
        
        assert model.activeView == 'instances'
        assert model.instances.size() == 2
        model.instances.eachWithIndex { inst, i ->
            assert inst.cloudId == cloudIds[i]
        }
    }
}
